const router = require('express').Router();
const multer  = require('multer');
const path = require('path');

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './public/uploads')
  },
  filename: function (req, file, cb) {
    // 自定义上传文件名
    const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9) + path.extname(file.originalname)
    cb(null, file.fieldname + '-' + uniqueSuffix)
  }
})

const upload = multer({ storage: storage })

// 渲染上传页面
router.get('/upload', (req,res) => {
  res.render('upload')
})
// 文件上传路由
router.post('/upload', upload.single('img') ,(req,res) => {
  res.send({
    code: 0,
    msg: '上传成功'
  })
})

module.exports = router;